package countDownLatch;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * @author:wesley
 */
public class MyThread extends Thread{

    private CountDownLatch comingTag;//所有运动员到达起跑线

    private CountDownLatch waitTag;//等待

    private CountDownLatch waitRunTag;//准备起跑

    private CountDownLatch beginTag;//开始

    private CountDownLatch endTag;//所有人员到达终点

    public MyThread(CountDownLatch comingTag, CountDownLatch waitTag, CountDownLatch waitRunTag,
                    CountDownLatch beginTag, CountDownLatch endTag) {
        this.comingTag = comingTag;
        this.waitTag = waitTag;
        this.waitRunTag = waitRunTag;
        this.beginTag = beginTag;
        this.endTag = endTag;
    }


    @Override
    public void run() {
        String tname = Thread.currentThread().getName();
        try {
            Thread.sleep((int)Math.random()*1000);
            System.out.println(tname + "运动员已经达到起跑线了");
            comingTag.countDown();

            System.out.println("等待准备开始");
            waitTag.await();


            System.out.println(tname+"姿势已经准备好了 。。。。");
            Thread.sleep((int)Math.random()*1000);
            waitRunTag.countDown();

            beginTag.await();
            System.out.println(tname+"正在奔跑ing。。。。");

            Thread.sleep((int)Math.random()*3000);
            System.out.println(tname+"已经到达终点!");
            endTag.countDown();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
